<?php
require_once 'Zend/Controller/Action.php';
class Administrator_BadgeController extends Zend_Controller_Action
{
	public function init()
	{
		$member=new CMember();
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){	
			$this->_redirect(MY_URL.'/administrator-auth-login');
		}
		$memberArray = $member->getUserDetails(array('uid' => $_COOKIE[COOKIE_PRE.'uid']));
    	if($_COOKIE[COOKIE_PRE.'uid']=='1'){
				
		} else {
        	$strarray = explode(",", $memberArray[0]['menuflag']);
        	
        	if (!in_array('12', $strarray)) {
        		exit('no priviage');
        	}
		}
	}
	
	public function preDispatch()
	{
		$this->getHelper ('layout')->setLayout('framelayout');
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
	}
	
	public function indexAction()
	{
		$badge = new CBadge();
		$this->view->badge = $badge->fetchAll();
	}
	
	public function addAction()
	{
		if ($this->getRequest()->isPost()) {
	    	$badge = new CBadge();
	    	$select_badgepatch = array();
	    	$paramArray=$this->_request->getParams();
	    	$filters = array('*'=> 'StringTrim');
	        $validators = array();
        	$input = new Zend_Filter_Input($filters, $validators, $paramArray);
        	$name = $input->name;
        	$price = $input->price;
        	$img = '';
        	if ($input->badge_img) {
        		foreach ($input->badge_img as $v) {
        			$img .= '|' . $v;
        		}
        	}
        	
        	$img = substr($img, 1, strlen($img));
        	$row = array('badge_name'=>$name, 'badge_price'=>$price, 'img'=>$img);
        	if ($badge->insert($row)) {
        		$this->getHelper('viewRenderer')->setNoRender();
        		$this->view->showMsg("添加成功",$_SERVER['HTTP_REFERER']);
        	} else {
        		$this->view->showMsg("添加失败",$_SERVER['HTTP_REFERER']);
        	}
		}
	}
	
	public function editAction()
	{
		$id = $this->getRequest()->getParam('id');
		$badge = new CBadge();
		
		if ($this->getRequest()->isPost()) {	    	
	    	$paramArray=$this->_request->getParams();
	    	$filters = array('*'=> 'StringTrim');
	        $validators = array();
        	$input = new Zend_Filter_Input($filters, $validators, $paramArray);
        	$name = $input->name;
        	$price = $input->price;
        	$img = '';
        	if ($input->badge_img) {
        		foreach ($input->badge_img as $v) {
        			$img .= '|' . $v;
        		}
        	}
        	
        	$img = substr($img, 1, strlen($img));
        	$row = array('badge_name'=>$name, 'badge_price'=>$price, 'img'=>$img);
        	$where = $badge->getAdapter()->quoteInto('badge_id = ?', $id);
        	
        	if ($badge->update($row, $where)) {
        		$this->getHelper('viewRenderer')->setNoRender();
        		$this->view->showMsg("修改成功",$_SERVER['HTTP_REFERER']);
        	} else {
        		$this->view->showMsg("修改失败",$_SERVER['HTTP_REFERER']);
        	}
		}
		
		$row = $badge->find($id)->current();
		$imgArr = explode('|', $row->img);
		if (empty($imgArr[0])) {
			$imgArr = array();
		}
		$this->view->badge = $row;
		$this->view->imgArr = $imgArr;
		$this->view->id = $id;
	}
	
	public function deleteAction()
	{
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		$id = $this->getRequest()->getParam('id', 0);
		$badge = new CBadge();
		$db = $badge->getAdapter();
		$where = $db->quoteInto('badge_id = ?', $id);
		if ($badge->delete($where)) {
			echo 1;
		} else {
			echo 0;
		}
	}
	
	public function uploadimg1Action()
	{
		header("Content-type: text/html; charset=utf-8");
        $this->getHelper ('layout')->disableLayout ();
        $upload=new CUpload();
        $path=new CPath();
        $thisPath=$path->getPathFromType('temp');
        $imageArray=$upload->uploadFile($thisPath,null,null,'500000');
        $isSuccess=false;
        if (is_array($imageArray)) {
            $isSuccess=true;
            $imgUrl=$path->toDbPath('temp',$imageArray[0]);
            $this->view->assign('imgUrl',$imgUrl);
        } else {
            $error=$imageArray;
            $this->view->assign('error',$error);
        }
        $this->view->assign('isSuccess',$isSuccess);
	}
}